﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Data.EF;
using Icoders.MarketTracker.Reports.Lib.Domain.Transactions;

namespace Icoders.MarketTracker.Reports.Lib.Repository.Transactions.Impl
{
   public class PurchaseReportRepository : IPurchaseReportRepository
   {
       private AppDataContext _ctx;

       public PurchaseReportRepository(AppDataContext ctx)
       {
           _ctx = ctx;
       }

       public List<PurchaseReport> GetReport(DateTime? startDate, DateTime? endDate)
       {
          return _ctx.TblPurchaseItem.Select(s => new PurchaseReport
                                                {
                                                    Product=s.TblProduct.Name,
                                                    Quantity=s.Quantity.Value,
                                                }).ToList();
       }

       public List<SaleReport> GetSaleReport(DateTime? startDate, DateTime? endDate)
       {
           IQueryable<TblPurchaseItem> query = _ctx.TblPurchaseItem;
           if (startDate != null && endDate != null)
           {
               endDate = endDate.Value.AddDays(1);
               query = query.Where(s => s.TblPurchase.CreatedOn >= startDate.Value && s.TblPurchase.CreatedOn <= endDate.Value);
           }
           return query.Select(s => new SaleReport
           {
               Product = s.TblProduct.Name,
               Quantity = s.Quantity.Value,
               SaleRep=s.TblPurchase.TblUser.Username,
               Area=s.TblPurchase.TblPointOfPurchase.TblZone.TblArea.Name,
               Pop=s.TblPurchase.TblPointOfPurchase.Name,
               Date=s.TblPurchase.CreatedOn
           }).ToList();
            
       }
   }
}
